import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigModule, ConfigService } from '@nestjs/config';
import {
  ROOT_ROLE_ID,
  SYS_TASK_QUEUE_NAME,
  SYS_TASK_QUEUE_PREFIX,
} from 'src/modules/admin/admin.constants';

import { BullModule } from '@nestjs/bull';
import { ConfigurationKeyPaths } from '@/config/configuration';
import { Module } from '@nestjs/common';
import SysCategory from 'src/entities/admin/sys-category.entity';
import { SysCategoryController } from './category/category.controller';
import { SysCategoryService } from './category/category.service';
import SysConfig from 'src/entities/admin/sys-config.entity';
import SysFontList from 'src/entities/admin/sys-font-list.entity';
import { SysFontlistController } from './fontlist/fontlist.controller';
import { SysFontlistService } from './fontlist/fontlist.service';
import { SysLogController } from './log/log.controller';
import { SysLogService } from './log/log.service';
import SysLoginLog from 'src/entities/admin/sys-login-log.entity';
import { SysOnlineController } from './online/online.controller';
import { SysOnlineService } from './online/online.service';
import { SysParamConfigController } from './param-config/param-config.controller';
import { SysParamConfigService } from './param-config/param-config.service';
import SysRole from 'src/entities/admin/sys-role.entity';
import { SysRoleController } from './role/role.controller';
import { SysRoleService } from './role/role.service';
import { SysServeController } from './serve/serve.controller';
import { SysServeService } from './serve/serve.service';
import SysTag from '@/entities/admin/sys-tags.entity';
import { SysTagController } from './tag/tag.controller';
import { SysTagService } from './tag/tag.service';
import SysUser from 'src/entities/admin/sys-user.entity';
import { SysUserController } from './user/user.controller';
import SysUserRole from 'src/entities/admin/sys-user-role.entity';
import { SysUserService } from './user/user.service';
import { TypeOrmModule } from '@nestjs/typeorm';
// import { WSModule } from 'src/modules/ws/ws.module';
import { rootRoleIdProvider } from '../core/provider/root-role-id.provider';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'CANVAS_SERVICE',
        transport: Transport.TCP,
        options: {
          port: 8888,
        },
      },
    ]),
    // 只要是以 entity.ts 结尾的实例类，都会被自动扫描识别，并在数据库中生成对应的实体表, HRM热更新时无效, 需要手动引入
    TypeOrmModule.forFeature([
      SysUser,
      SysUserRole,
      SysRole,
      SysConfig,
      SysUserRole,
      SysLoginLog,
      SysTag,
      SysCategory,
      SysFontList,
    ]),
    BullModule.registerQueueAsync({
      name: SYS_TASK_QUEUE_NAME,
      imports: [ConfigModule],
      useFactory: (configService: ConfigService<ConfigurationKeyPaths>) => ({
        redis: {
          host: configService.get<string>('redis.host'),
          port: configService.get<number>('redis.port'),
          password: configService.get<string>('redis.password'),
          db: configService.get<number>('redis.db'),
        },
        prefix: SYS_TASK_QUEUE_PREFIX,
      }),
      inject: [ConfigService],
    }),
    // WSModule,
  ],
  controllers: [
    SysUserController,
    SysRoleController,
    SysLogController,
    SysOnlineController,
    SysParamConfigController,
    SysServeController,
    SysTagController,
    SysCategoryController,
    SysFontlistController,
  ],
  providers: [
    rootRoleIdProvider(),
    SysUserService,
    SysRoleService,
    SysParamConfigService,
    SysLogService,
    SysOnlineService,
    SysServeService,
    SysTagService,
    SysCategoryService,
    SysFontlistService,
  ],
  exports: [ROOT_ROLE_ID, TypeOrmModule, SysUserService, SysLogService, SysOnlineService],
})
export class SystemModule {}
